package com.colde.salary.webservice.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.colde.salary.common.RequestDTO;
import com.colde.salary.common.ServerResponse;
import com.colde.salary.model.User;
import com.colde.salary.service.IUserService;
import com.colde.salary.util.SerialUtil;

@RestController
@RequestMapping("/user")
public class UserController {
	
	public Logger LOGGER = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private IUserService userService;
	
	@PostMapping("/add")
	@Transactional
	public ServerResponse<User> addUser(@RequestBody @Valid User user, BindingResult result) {
		if(result.hasErrors()) {
			LOGGER.info("@Valid:"+result.getFieldError().getDefaultMessage());
			return ServerResponse.createByErrorMessage(result.getFieldError().getDefaultMessage());
		}
		user.setUserId(SerialUtil.getUid());
		user.setCreatetime(new Date());
		user.setUpdatetime(new Date());
		int a=  userService.insert(user);
		
		user.setUserId(SerialUtil.getUid());
		user.setSex("女女女");
		int b=  userService.insert(user);	
		return ServerResponse.createBySuccessMessage(String.valueOf(a+b));
	}
	
	@PostMapping("/findList")
	public ServerResponse<List<User>> findList(@RequestBody RequestDTO requestDTO) {
		String role = requestDTO.getParam("role");
		return userService.findUserList(role);
	}
	
	@PostMapping("/login")
	public boolean login(@RequestBody User user) {
		return userService.login(user);
	}
	
	@PostMapping("/export")
	public void  exportUserExcel(HttpServletResponse response) {
		userService.export(response);
	}
	
	@PostMapping("/import")
	public void  exportUserExcel(@RequestParam MultipartFile file) {
		userService.importUser(file);
	}
}
